<?php
/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/7/18
 * Time: 11:01
 */

namespace App\Business\Article;


use App\Business\BusBase;
use App\Business\ReturnResult;
use App\Models\articles;
use Illuminate\Support\Facades\DB;
use Symfony\Component\DomCrawler\Crawler;

class CreateArticle extends BusBase
{
    use ArticleTypeEnum;



    public $articleTitle;
    public $articleContent;
    public $articleWeight;
    public $type;

    protected $type_number=0;


    /**
     * 检测参数是否符合要求
     */
    protected function checkParameters()
    {
        if(!$this->articleTitle) return ReturnResult::ParameterError(null,"文章标题不能为空");
        if(!$this->type||!array_key_exists(strtolower($this->type),self::$ArrayType)) return ReturnResult::ParameterError(null,"文章类型错误");
        $this->type_number=self::$ArrayType[strtolower($this->type)];

        return ReturnResult::Success();

    }

    protected function Handle()
    {

        $crawler=new Crawler();
        $crawler->addHtmlContent($this->articleContent);
        $contenttext=$crawler->text();

        DB::transaction(function ()use($contenttext){

            $model=new articles();
            $model->type=$this->type_number;
            $model->title=$this->articleTitle;
            $model->content=$this->articleContent;
            $model->contentText=$contenttext;
            $model->weight=$this->articleWeight;
            $model->save();
        });

        return ReturnResult::Success();


    }

    public function GetBusinessName()
    {
        return "创建文章";
    }


}